import { Component, OnInit } from '@angular/core';
import { HttpService } from '../http.service';
import {utcToDate, loadMore} from '../../assets/js/utils';

@Component({
  selector: 'app-account-detail',
  templateUrl: './account-detail.component.html',
  styleUrls: ['./account-detail.component.less']
})
export class AccountDetailComponent implements OnInit {
  list = [];
  currentType=0;
  types=['全部','推荐奖励','提现','直推分润','代理分润']
  utcToDate = utcToDate;
  page = 1;

  loadMore = new loadMore({cb: this.nextPage.bind(this)});


  constructor(private http: HttpService) {
    this.getData()
  }

  clickType(i){
    this.currentType = i
    this.page = 1
    console.log('关闭下拉加载')
    this.loadMore.close()
    this.list = []
    this.getData()
  }

  async nextPage(){
    this.page++
    await this.getData()
  }

  async getData(){
    const {success, data, msg} = await this.http.post({
      name: 'car.get.score.log',
      type: this.currentType,
      page: this.page
    })
    this.loadMore.loading = false
    if(Array.isArray(data)) this.list.push(...data)
    setTimeout(()=>{
      if(document.body.scrollHeight > window.innerHeight && this.page == 1)
        this.loadMore.open()
      else if(!data.length)
        this.loadMore.close()
      }, 100)
    }

  ngOnInit() {
  }

  ngOnDestroy() {
    this.loadMore.close()
  }

}
